package com.milkTeaServer.controller;

import com.milkTeaServer.bean.Cart;
import com.milkTeaServer.bean.Comment;
import com.milkTeaServer.bean.MilkTea;
import com.milkTeaServer.dao.CartDao;
import com.milkTeaServer.dao.CommentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentDao dao;

    @RequestMapping("/add")
    @ResponseBody
    public Map addComment(@RequestBody Comment comment) {
        Map map = new HashMap();
        Comment comment1 = dao.saveAndFlush(comment);
        if (comment1 != null) {
            map.put("success", true);
            map.put("massage", "发表评论成功!");
            map.put("data", comment1);
        } else {
            map.put("success", false);
            map.put("massage", "发表评论失败!");
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Map findCommentListByMilkTeaId(@RequestBody MilkTea milkTea) {
        Map map = new HashMap();
        List<Comment> commentList = dao.findByMilkTeaId(milkTea.getId());
        if (commentList != null) {
            map.put("success", true);
            map.put("massage", "查询评论成功!");
            map.put("data", commentList);
        } else {
            map.put("success", false);
            map.put("massage", "查询评论失败!");
            map.put("data", null);
        }
        return map;
    }
}
